package io.gbznt.modules.buss.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.gbznt.common.utils.PageUtils;
import io.gbznt.common.utils.R;
import io.gbznt.common.utils.ShiroUtils;
import io.gbznt.common.validator.ValidatorUtils;
import io.gbznt.common.validator.group.AddGroup;
import io.gbznt.modules.buss.entity.BussFarmingActivityEntity;
import io.gbznt.modules.buss.entity.BussFarmlandInfoEntity;
import io.gbznt.modules.buss.service.BussFarmingActivityService;
import io.gbznt.modules.buss.service.BussFarmlandInfoService;
import io.gbznt.modules.sys.entity.SysUserEntity;
import io.gbznt.modules.sys.service.SysUserService;



/**
 * 农事活动
 *
 * @author gbznt
 * @email http://www.gbznt.com/
 * @date 2019-04-20 15:48:34
 */
@RestController
@RequestMapping("buss/bussfarmingactivity")
public class BussFarmingActivityController {
    @Autowired
    private BussFarmingActivityService bussFarmingActivityService;
    @Autowired
    private SysUserService sysuser;
    @Autowired
    private BussFarmlandInfoService bindo;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("buss:bussfarmingactivity:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = bussFarmingActivityService.queryPage(params);

        return R.ok().put("page", page);
    }
    
    /**
     * 列表
     */
    @RequestMapping("/list2")
    @RequiresPermissions("buss:bussfarmingactivity:list")
    public R list2(@RequestParam Map<String, Object> params){
        PageUtils page = bindo.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("buss:bussfarmingactivity:info")
    public R info(@PathVariable("id") String id){
        BussFarmingActivityEntity bussFarmingActivity = bussFarmingActivityService.getById(id);
        //负责人
        String fzr  = bussFarmingActivity.getHandler();
        //田块
        String tk = bussFarmingActivity.getFarmlandInfoId();
        if(StringUtils.isNotBlank(fzr)){
        	SysUserEntity suser =  sysuser.getById(fzr);
        	if(suser != null){
        		bussFarmingActivity.setHandlername(suser.getName());
        	}
        	
        }
        
        if(StringUtils.isNotBlank(tk)){
        	BussFarmlandInfoEntity suser =  bindo.getById(tk);
        	if(suser != null){
        		bussFarmingActivity.setFarmlandInfoIdname(suser.getName());
        	}
        	
        }
        
        

        return R.ok().put("bussFarmingActivity", bussFarmingActivity);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("buss:bussfarmingactivity:save")
    public R save(@RequestBody BussFarmingActivityEntity bussFarmingActivity){
    	ValidatorUtils.validateEntity(bussFarmingActivity,AddGroup.class);
    
   	 	bussFarmingActivity.setCreateId(ShiroUtils.getUserId()+"");
    	bussFarmingActivity.setCreateTime(new Date());
    	//添加部门 (田块的所在基地)
    	String fzr = bussFarmingActivity.getFarmlandInfoId();
    	BussFarmlandInfoEntity bfe =  bindo.getById(fzr);
    	if(bfe!=null){
    		bussFarmingActivity.setDeptId(bfe.getDeptId());
    	}
    	
    	
    	/*SysUserEntity user = sysuser.selectById(fzr);
    	if(user!=null){
    		bussFarmingActivity.setDeptId(user.getDeptId());
    	}*/
    	
    	
        bussFarmingActivityService.save(bussFarmingActivity);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("buss:bussfarmingactivity:update")
    public R update(@RequestBody BussFarmingActivityEntity bussFarmingActivity){
        ValidatorUtils.validateEntity(bussFarmingActivity,AddGroup.class);
       
        bussFarmingActivity.setUpdateId(ShiroUtils.getUserId()+"");
    	bussFarmingActivity.setUpdateTime(new Date());
    	//添加部门 (田块的所在基地)
    	String fzr = bussFarmingActivity.getFarmlandInfoId();
    	BussFarmlandInfoEntity bfe =  bindo.getById(fzr);
    	if(bfe!=null){
    		bussFarmingActivity.setDeptId(bfe.getDeptId());
    	}
    	
        Boolean re = bussFarmingActivityService.updateById(bussFarmingActivity);//全部更新
        
        return re?R.ok():R.oldVersion();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("buss:bussfarmingactivity:delete")
    public R delete(@RequestBody String[] ids){
        bussFarmingActivityService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
